<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: IndexController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Kowcore_IndexController extends Core_Controller_Action_Standard {
	public function likeAction() {
		if (! $this->_helper->requireUser ()->isValid ()) {
			return;
		}
		
		$guid = $this->_getParam ( 'guid' );
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$subject = Engine_Api::_ ()->getItemByGuid ( $guid );
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $subject, $viewer, 'comment' )->isValid ()) {
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid request method' );
			return;
		}
		
		// Process
		$db = $subject->likes ()->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$subject->likes ()->addLike ( $viewer );
			
			// Add notification
			$subjectOwner = $subject->getOwner ();
			if ($subjectOwner->getType () == 'user' && $subjectOwner->getIdentity () != $viewer->getIdentity ()) {
				$notifyApi = Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' );
				$notifyApi->addNotification ( $subjectOwner, $viewer, $subject, 'liked', array ('label' => $subject->getShortType () ) );
			}
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	}
	
	public function unlikeAction() {
		if (! $this->_helper->requireUser ()->isValid ()) {
			return;
		}
		
		$guid = $this->_getParam ( 'guid' );
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$subject = Engine_Api::_ ()->getItemByGuid ( $guid );
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $subject, $viewer, 'comment' )->isValid ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You can not unlike or like this' );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid request method' );
			return;
		}
		
		// Process
		$db = $subject->likes ()->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$subject->likes ()->removeLike ( $viewer );
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	}
	
	public function noteAction() {
		$guid = $this->_getParam ( 'guid' );
		$note = $this->_getParam ( 'note' );
		
		if (! $guid) {
			$this->view->error = $this->view->translate ( 'Wrong arguments.' );
			return;
		}
		
		$subject = Engine_Api::_ ()->getItemByGuid ( $guid );
		
		if (! $subject->getIdentity ()) {
			$this->view->error = $this->view->translate ( 'Wrong arguments.' );
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if (! $subject->authorization ()->isAllowed ( $viewer, 'edit' )) {
			$this->view->error = $this->view->translate ( 'You have no permissions to do that.' );
			return;
		}
		
		Engine_Api::_ ()->kowcore ()->postNote ( $subject, $note );
		$this->view->success = $this->view->translate ( 'Note was posted.' );
	}
	
	public function hintAction() {
		$guid = $this->_getParam ( 'guid', '' );
		if (! $guid) {
			return;
		}
		
		$object = Engine_Api::_ ()->getItemByGuid ( $guid );
		$type = $object->getType ();
		switch ($type) {
			case 'event' :
				$items = $object->membership ()->getMembers ();
				$total = count ( $items );
				$label = '%s user are going to "%s" event';
				$label2 = '%s users are going to "%s" event';
				break;
			case 'group' :
				$items = $object->membership ()->getMembers ();
				$total = count ( $items );
				$label = '%s user are members of "%s" group';
				$label2 = '%s users are members of "%s" group';
				break;
			case 'user' :
				$items = $object->membership ()->getMembers ();
				$total = count ( $items );
				$label = '%s user are friends of %s';
				$label2 = '%s users are friends of %s';
				break;
			case 'contest' :
				$items = $object->getParticipants ();
				$total = $items->getTotalItemCount ();
				$label = '%s user participated in "%s" contest';
				$label2 = '%s users participated in "%s" contest';
				break;
		}
		
		$this->view->html = $this->view->partial ( 'helper/hint.tpl', 'kowcore', array ('object' => $object, 'items' => $items, 'total' => $total, 'label' => $label, 'label2' => $label2 ) );
	}
	
	public function rateAction() {
		$guid = $this->_getParam ( 'guid', '' );
		if (! $guid) {
			return;
		}
		
		$score = $this->_getParam ( 'score', 0 );
		if (! $score) {
			return;
		}
		
		$object = Engine_Api::_ ()->getItemByGuid ( $guid );
		if (! $object->getIdentity ()) {
			return;
		}
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if ($object->isOwner ( $viewer )) {
			return;
		}
		
		if (Engine_Api::_ ()->kowcore ()->rateObject ( $score, $object )) {
			$info = Engine_Api::_ ()->kowcore ()->getRateInfo ( $object );
			
			if (isset ( $object->vote_count )) {
				$object->vote_count ++;
				$object->save ();
			}
			
			$this->view->score = round ( $info ['score'], 2 );
			$this->view->count = $info ['cnt'];
			
			$this->view->status = 1;
			$this->view->msg = $this->view->translate ( 'You have just rated %s.', $object->getTitle () );
		} else {
			$this->view->status = 0;
			$this->view->msg = $this->view->translate ( 'There was an error. Probably you already rate this.' );
		}
	}

}